package com.kgc.controller;

import com.kgc.service.IOutOrderConsumerService;
import entity.ResponseMessage;
import entity.StatusCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @description: outorder 消费者controller
 *
 * @author: Cheng
 *
 * @create: 2022-04-20 14:26
 **/
@Controller
@CrossOrigin
    @RequestMapping("outorder")
public class OutOrderConsumerController {


    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private IOutOrderConsumerService iOutOrderConsumerService;
    /**
     * 按条件查询门店订单信息
     * @param orderNum
     * @param sname
     * @param startTime
     * @param endTime
     * @param status
     * @param pageNum
     * @return
     */
    @RequestMapping("selectOutOrderInfo")
    @ResponseBody
    public ResponseMessage selectOutOrderInfo(@RequestParam(value = "orderNum",required = false) String orderNum,
                                              @RequestParam(value = "sname",required = false) String sname,
                                              @RequestParam(value = "startTime",required = false,defaultValue = "") String startTime,
                                              @RequestParam(value = "endTime",required = false,defaultValue = "") String endTime,
                                              @RequestParam(value = "status",required = false,defaultValue = "-1") int status,
                                              @RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum
    ) {
        logger.info("OutOrderConsumerController selectOutOrderInfo is start ... params:orderNum:-->" + orderNum + ",sname:-->" + sname + ",startTime:-->" + startTime + ",endTime:-?" + endTime + ",status:->" + status + ",pageNum:->" + pageNum);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ResponseMessage responseMessage = null;
        Date startTimeDate=null;
        Date endTimeDate = null;
        try {
            if (startTime != null && !"".equals(startTime)) {
                startTimeDate = simpleDateFormat.parse(startTime);
            }
            if (endTime != null && !"".equals(endTime)) {
                endTimeDate = simpleDateFormat.parse(endTime);
            }
            responseMessage = iOutOrderConsumerService.selectOutOrderInfo(orderNum, sname, startTimeDate, endTimeDate, status, pageNum);
            logger.debug("OutOrderConsumerController selectOutOrderInfo result:" + responseMessage);
        } catch (ParseException e) {
            e.printStackTrace();
            logger.error("OutOrderConsumerController selectOutOrderInfo is error", e);
            responseMessage.setMessage("传入的日期格式不对!");
            responseMessage.setCode(StatusCode.ERROR);
            return responseMessage;
        }
        return responseMessage;
    }


    /**
     * 通过OutOrderInfo的id查询    联表 product
     * @param id
     * @return ResponseMessage
     */
    @RequestMapping("selectProductAndOutOrderInfo")
    @ResponseBody
    public ResponseMessage selectProductAndOutOrderInfo(@RequestParam("id") String id) {
        logger.info("OutOrderConsumerController selectProductAndOutOrderInfo is start...param:id-->" + id);
        ResponseMessage responseMessage = iOutOrderConsumerService.selectProductAndOutOrderInfo(id);
        logger.debug("OutOrderConsumerController selectProductAndOutOrderInfo result:" + responseMessage);
        return responseMessage;
    }



}
